package com.wang.hashmap;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 存储结构,哈希表(数组+链表+(JDK1.8之后加入红黑树))
 */
public class HashMapDemo {
    public static void main(String[] args) {
        HashMap<User,String> hashMap = new HashMap<>();
        //1.添加元素
        User u1 = new User("wang1",1);
        User u2 = new User("wang2",2);
        User u3 = new User("wang3",3);
        hashMap.put(u1,"1");
        hashMap.put(u2,"2");
        hashMap.put(u3,"3");
        //问题来了,通过new对象还是破坏了键的唯一性,在堆中是不同的对象,所以加进来了,我们要通过user信息判断唯一性
        //在hashset说过,hash表是通过hashCode和equals进行判断重复
        //我们还是要重写hashCode和equals(通过姓名和stuNo判断重复)
        hashMap.put(new User("wang3",3),"4");//重写之后这个4将会覆盖之前的3
        System.out.println(hashMap.size()+"个,"+hashMap.toString());
        //3.删除
//        hashMap.remove(u1);
        //遍历
        Set<User> users = hashMap.keySet();
        for (User user : users) {
            System.out.println(user+","+hashMap.get(user));
        }
        //entrySet.
        Set<Map.Entry<User, String>> entries = hashMap.entrySet();
        for (Map.Entry<User, String> entry : entries) {
            System.out.println(entry.getKey()+","+entry.getValue());
        }
        //判断
        if (hashMap.containsKey(new User("wang1",1) ))System.out.println("yes");
    }
}
